<?php

class Service_Attach
{
    private $oss;
    private $bucket;

    public function __construct()
    {
        $this->oss = new Service_Oss();
        $this->bucket = 'yfb';
    }

    /**
     * @param string $conf
     *
     * @return Service_Attach
     */
    public static function getInstance($conf = 'oss')
    {
        static $oss = array();
        if (isset($oss[$conf])) {
            return $oss[$conf];
        } else {
            $oss[$conf] = new self;
            return $oss[$conf];
        }
    }

    /**
     * @return string
     */

    /**
     * @param string $sAvatar_url 原来头像链接，如果有，就把原来的删除
     * @return string
     */
    public function uploadAvatar($sAvatar_url = '')
    {
        return $this->uploadOSS('avatar', 'avatar', 'jpg', $sAvatar_url);
    }


    /**
     * @param string $sAvatar_url 原来头像链接，如果有，就把原来的删除
     * @return string
     */
    public function uploadAvatarNew($sAvatar_url = '')
    {
        return $this->uploadOSS('file', 'avatar', 'jpg', $sAvatar_url);
    }

    /**
     * @param string $sPhoto_url 原来相册链接，如果有，就把原来的删除
     * @return string
     */
    public function uploadPhotoNew($sPhoto_url = '')
    {
        return $this->uploadOSS('file', 'photo', 'jpg', $sPhoto_url);
    }
    public function uploadtousuPhoto($sPhoto_url = '')
    {
        return $this->uploadOSS('file', 'tousu', 'jpg', $sPhoto_url);
    }
    public function uploadAvatarBig()
    {
        return $this->uploadOSS('avatar_big', 'avatar_big');
    }

    public function uploadOSS($input_file_field = 'pic', $save_dir = 'pic', $ext = 'jpg', $sAvatar_url = '')
    {

        $file_path = $_FILES[$input_file_field]["tmp_name"];

        if (!$file_path) {
            FLogger::write('tmp_name为空', 'update_mumu');
            return '';
        }

        $_file_path_info = pathinfo($_FILES [$input_file_field]['name']);
        $file_ext = strtolower($_file_path_info['extension']);

        if (!$file_ext) {
            $this->get_extension($file_path);
        }

        if (!$file_ext) $file_ext = $ext;

        $content_type = MimeTypes::get_mimetype(strtolower($file_ext));

        if ($content_type == 'application/vnd.android.package-archive') {
            return '';
        } else {

            $object = 'oss/' . $save_dir . '/' . date('Ym/d/His') . rand(1000, 9999) . '.' . $file_ext;

            $flag = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object, $file_path, $content_type);

            if ($flag) {
                if ($sAvatar_url) {
                    //删除原来的图片
                    if ($sAvatar_url != $object) {
                        $this->deletePic($sAvatar_url);
                    }
                }
                return $object;
            } else {
                return '';
            }
        }
    }

    public function uploadVoiceMp3AndAmr()
    {

        $file_path_mp3 = $_FILES['file_voice_mp3']["tmp_name"];
        $file_path_amr = $_FILES['file_voice_amr']["tmp_name"];

        if (!$file_path_mp3 && !$file_path_amr) {
            FLogger::write('tmp_name为空', 'update_mumu');
            return '';
        }

        $_file_path_info_mp3 = pathinfo($_FILES ['file_voice_mp3']['name']);
        $_file_path_info_amr = pathinfo($_FILES ['file_voice_amr']['name']);

        $file_ext_mp3 = strtolower($_file_path_info_mp3['extension']);
        $file_ext_amr = strtolower($_file_path_info_amr['extension']);

        if (!$file_ext_mp3 && !$file_ext_amr) {
            $this->get_extension($file_path_mp3);
            $this->get_extension($file_path_amr);
        }

//        if (!$file_ext) $file_ext = $ext;

//        $content_type = MimeTypes::get_mimetype(strtolower($file_ext));
        $content_type_mp3 = MimeTypes::get_mimetype(strtolower($file_ext_mp3));
        $content_type_amr = MimeTypes::get_mimetype(strtolower($file_ext_amr));

        if ($content_type_mp3 == 'application/vnd.android.package-archive' && $content_type_amr == 'application/vnd.android.package-archive') {
            return '';
        } else {
            $file_name_date = date('Ym/d/His');
            $file_name_rand = rand(1000, 9999);

            $object_mp3 = 'oss/' . 'voice' . '/' . $file_name_date . $file_name_rand . '.' . $file_ext_mp3;
            $object_amr = 'oss/' . 'voice' . '/' . $file_name_date . $file_name_rand . '.' . $file_ext_amr;

            $flag_mp3 = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object_mp3, $file_path_mp3, $content_type_mp3);
            $flag_amr = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object_amr, $file_path_amr, $content_type_amr);

            if ($flag_mp3 && $flag_amr) {
                $objectArray = explode('.', $object_mp3);
                return $objectArray[0];
            } else {
                return '';
            }
        }
    }


    public function uploadVideoMp4()
    {

        $file_path_mp4 = $_FILES['file_video_mp4']["tmp_name"];

        if (!$file_path_mp4) {
            FLogger::write('tmp_name为空', 'update_mumu');
            return '';
        }

        $_file_path_info_mp4 = pathinfo($_FILES ['file_video_mp4']['name']);

        $file_ext_mp4 = strtolower($_file_path_info_mp4['extension']);

        if (!$file_ext_mp4) {
            $this->get_extension($file_ext_mp4);
        }

//        if (!$file_ext) $file_ext = $ext;

//        $content_type = MimeTypes::get_mimetype(strtolower($file_ext));
        $content_type_mp4 = MimeTypes::get_mimetype(strtolower($file_ext_mp4));

        if ($content_type_mp4 == 'application/vnd.android.package-archive') {
            return '';
        } else {
            $file_name_date = date('Ym/d/His');
            $file_name_rand = rand(1000, 9999);

            $object_mp4 = 'oss/' . 'voice' . '/' . $file_name_date . $file_name_rand . '.' . $file_ext_mp4;

            $flag_mp4 = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object_mp4, $file_path_mp4, $content_type_mp4);

            if ($flag_mp4) {
//                $objectArray = explode('.', $object_mp4);
                return $object_mp4;
            } else {
                return '';
            }
        }
    }

    public function uploadOSSByFile($imgUrl, $save_dir = 'pic')
    {
        $file_ext = 'jpg';
        $object = 'oss/' . $save_dir . '/' . date('Ym/d/His') . rand(1000, 9999) . '.' . $file_ext;
        $content_type = MimeTypes::get_mimetype(strtolower($file_ext));
        if ($content_type == 'application/vnd.android.package-archive') {
            return '';
        } else {
            $flag = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object, $imgUrl, $content_type);

            if ($flag) {
                return $object;
            } else {
                return '';
            }
        }
    }

    public function uploadPhoto($input_file_field = 'userfile')
    {
        return $this->uploadOSS($input_file_field, 'photo');
    }

    public function getPhotoUrl($photo_url, $w = 0, $h = 0)
    {
        return $this->getThumbUrl($photo_url, $w, $h);
    }

    public function getThumbUrl($thumb_url, $w = 0, $h = 0)
    {
        global $_F;
        if (substr($thumb_url, 0, 4) == 'oss/') {

            if (strpos($thumb_url, 'ttp://')) {
                $ret = $thumb_url;
            } else {
                $ret = $_F['s_url_oss'] . '/uploads/' . $thumb_url;
            }

            if ($w > 0 && $h > 0) {
                $ret .= '@1e_' . $w . 'w_' . $h . 'h_1c_0i_1o_90Q_1x.jpg';
            }

        } else {

            if (strpos($thumb_url, 'ttp://')) {
                $ret = $thumb_url;
            } else {
                $ret = $_F['s_url'] . '/uploads/' . $thumb_url;
            }

            if ($w > 0 && $h > 0) {
                $ret .= '_' . $w . '_' . $h . '.jpg';
            }

        }

        if (!$thumb_url) {
            $ret = "";
        }

        return $ret;
    }

    /**
     *
     * @param $img_src
     * @param $type 0、46、64、96、132 0代表640*640正方形头像
     * @return string
     */
    public static function wxHeadImgUrl($img_src,$type){
        $img_src = substr($img_src,0,strlen($img_src) - 1);
        return $img_src . $type;
    }

    public function get_extension($file)
    {
        return end(explode('.', $file));
    }

    /**删除图片
     * @param $object
     */
    public function deletePic($object)
    {
        global $_F;
        if ($object) {
            //删除阿里云原图片
            if (substr($object, 0, 4) == 'oss/') {
                $this->oss->delete_object($this->bucket, 'uploads/' . $object);
            } else {
                //删除我们自己服务器图片
                $module = $_F['module'];
                if ($module == 'upload') {
                    $file = UPLOAD_ROOT . $object;
                    $result = @unlink($file);
                    FLogger::write($file . '-' . $result, 'deletePic');
                } else {
                    FHttp::get('http://upload.img.yuanfenba.net/index/deletePic?url=' . urlencode($object));
                }
            }
        }
    }

    /**
     * 检测object是否存在
     * @param $object
     * @param bool $return 默认为false, 为true有返回bool
     * @return bool
     */
    public function is_object_exist($object,$return = false){
        return $this->oss->is_object_exist($this->bucket,$object,$return);
    }

    /*
     * 上传服务器文件至OSS
     *@param string  $file_name 文件绝对路径名 ， $save_name 保存的OSS路径名 ,  $ext 文件拓展名， $sAvatar_url 是否删除原来的图片
     * @return OSS文件路径名称
     */

    public function uploadFileOSS($file_name, $save_name, $ext, $sAvatar_url = '')
    {

        $file_path = $file_name;

        if (!$file_path) {
            FLogger::write('tmp_name为空', 'update_mumu');
            return '';
        }
        $extent = '';
        if (!$ext) {
            $extent = $this->get_extension($file_path);
        }

        if (!$extent) $extent = $ext;

        $content_type = MimeTypes::get_mimetype(strtolower($extent));

            $object = 'oss/' . $save_name . '.' . $extent;

            $flag = $this->oss->upload_by_file($this->bucket, 'uploads/' . $object, $file_path, $content_type);

            if ($flag) {
                if ($sAvatar_url) {
                    //删除原来的图片
                    if ($sAvatar_url != $object) {
                        $this->deletePic($sAvatar_url);
                    }
                }
                return $object;
            } else {
                return '';
            }

    }

}